package com.hd123.rumba.gwt.widget.client;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;

public class LabeledDoubleListBox extends HorizontalPanel implements LabeledBox {
  private Label label;
  private ListBox box1, box2;
  private String caption;
  private boolean required = false;
  private boolean readOnly = false;

  public LabeledDoubleListBox() {
    super();
    setSpacing(4);

    label = new Label();
    label.setWidth("100px");
    label.setHorizontalAlignment(Label.ALIGN_RIGHT);
    setCellVerticalAlignment(label, HorizontalPanel.ALIGN_MIDDLE);
    add(label);

    box1 = new ListBox();
    setCellVerticalAlignment(box1, HorizontalPanel.ALIGN_MIDDLE);
    add(box1);

    box2 = new ListBox();
    setCellVerticalAlignment(box2, HorizontalPanel.ALIGN_MIDDLE);
    add(box2);
  }

  public LabeledDoubleListBox(String caption) {
    this();
    setCaption(caption);
  }

  public LabeledDoubleListBox(String caption, String[] list1, String[] list2) {
    this(caption);
    addItems(list1);
    addItems2(list2);
  }

  public String getCaption() {
    return caption;
  }

  public void setCaption(String caption) {
    this.caption = caption;
    if (required && !readOnly)
      DOM.setInnerHTML(label.getElement(), this.caption + "<font color='red'>*</font>");
    else
      label.setText(caption);
  }

  public String getText() {
    return box1.getItemText(box1.getSelectedIndex());
  }

  public void setText(String text) {
    int count = box1.getItemCount();
    for (int i = 0; i < count; ++i) {
      if (box1.getItemText(i).equals(text)) {
        box1.setSelectedIndex(i);
        return;
      }
    }
  }

  public String getText2() {
    return box2.getItemText(box2.getSelectedIndex());
  }

  public void setText2(String text) {
    int count = box2.getItemCount();
    for (int i = 0; i < count; ++i) {
      if (box2.getItemText(i).equals(text)) {
        box2.setSelectedIndex(i);
        return;
      }
    }
  }

  public boolean getReadOnly() {
    return readOnly;
  }

  public void setReadOnly(boolean b) {
    readOnly = b;
    setCaption(caption);
    box1.setEnabled(!b);
    box2.setEnabled(!b);
  }

  public void setRequired(boolean required) {
    this.required = required;
    setCaption(caption);
  }

  public void setBoxMaxLength(int length) {
  }

  public void setCaptionWidth(String width) {
    label.setWidth(width);
  }

  public void setBoxWidth(String width) {
    box1.setWidth(width);
  }

  public void setBox2Width(String width) {
    box2.setWidth(width);
  }

  public void addItem(String text) {
    box1.addItem(text);
  }

  public void addItem2(String text) {
    box2.addItem(text);
  }

  public void addItems(String[] items) {
    for (int i = 0; items != null && i < items.length; ++i)
      box1.addItem(items[i]);
  }

  public void addItems2(String[] items) {
    for (int i = 0; items != null && i < items.length; ++i)
      box2.addItem(items[i]);
  }
}
